<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.0.0-alpha.1/axios.js"></script>
    <!-- mockjs -->
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
  </head>
  <body>
    <div id="app">
      <input v-model="val" type="text" />
      <div>
        <ul>
          <li v-for="item in list">{{ item.name }}</li>
        </ul>

        <p v-show="list.length === 0">暂无数据</p>
      </div>
    </div>
    <script>
      var re;

      //模拟随机条数随机数据接口
      Mock.mock("/api/getList", "get", {
        msg: "模拟的接口",
        "data|50": [
          {
            name: "@name",
          },
        ],
      });
      axios.get("/api/getList").then((res) => {
        console.log(res);
        re = res.data.data;
        console.log(re);
      });
      Mock.mock(RegExp("/api/search" + "*."), (req) => {
        console.log(req);
        let name = req.url.split("=")[1];
        let filterArr = re.filter((item) => item.name.includes(name));
        console.log(filterArr);
        return filterArr;
      });
      new Vue({
        el: "#app",
        data: {
          val: "",
          list: [],
        },
        watch: {
          val(name) {
            if (name.length === 0) {
              this.list = [];
            } else {
              axios
                .get("/api/search", {
                  params: {
                    name,
                  },
                })
                .then((res) => {
                  this.list = res.data;
                });
            }
          },
        },
      });
    </script>
  </body>
</html>
